package dir

import (
	"bufio"
	"os"
	"strings"
)

func ParseM3u8File(fullPath string) ([]string, error) {
	f, err := os.Open(fullPath)
	if err != nil {
		return nil, err
	}
	defer f.Close()
	s := bufio.NewScanner(f)
	files := make([]string, 0)
	for s.Scan() {
		line := s.Text()
		if strings.HasPrefix(line, "#") {
			continue
		}
		//file := path.Join(dir, line)
		files = append(files, line)
	}
	err = s.Err()
	return files, nil
}

func Increment(fullPath string, oldCount int) (inc []string, resultLen int, err error) {
	iterms, err := ParseM3u8File(fullPath)
	inc = make([]string, 0)
	resultLen = len(iterms)

	//fmt.Println("items len ", len(iterms))
	if err == nil && len(iterms) >= oldCount {
		return iterms[oldCount:], resultLen, nil
	}
	return
}
